package com.chinapost.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinapost.dao.BaseDaoI;
import com.chinapost.po.Tycsj;
import com.chinapost.service.YcsjServiceI;
import com.chinapost.vo.DataGrid;
import com.chinapost.vo.Ycsj;

@Service("ycsjService")
public class ycsjServiceImpl implements YcsjServiceI {

	@Autowired
	private BaseDaoI<Tycsj> ycsjDao;
	
	@Override
	public DataGrid datagrid(Ycsj ycsj) {
		DataGrid dg = new DataGrid();
		dg.setRows(listRows(find(ycsj)));
		dg.setTotal(total(ycsj));
		return dg;
	}

	private Long total(Ycsj ycsj) {
		String hql = "select count(*) from Tycsj t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(ycsj, hql, params);
		return ycsjDao.count(hql, params);
	}

	private List<Tycsj> find(Ycsj ycsj) {
		String hql = "from Tycsj t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(ycsj, hql, params);
		if (ycsj.getOrder() != null && ycsj.getSort() != null) {
			hql += " order by " + ycsj.getSort() + " " + ycsj.getOrder();
		}
		return ycsjDao.find(hql, params, ycsj.getPage(), ycsj.getRows());
	}

	private List<Ycsj> listRows(List<Tycsj> lt) {
		List<Ycsj> rows = new ArrayList<Ycsj>();
		if (lt != null && lt.size() > 0) {
			for (Tycsj t : lt) {
				Ycsj sub = new Ycsj();
				BeanUtils.copyProperties(t, sub);
				sub.setCzh("**" + t.getCzh().substring(t.getCzh().length() - 7));
				sub.setDeptid(t.getTdept().getCid());
				sub.setDeptname(t.getTdept().getCname());
				sub.setKindid(t.getTkind().getCid());
				sub.setKindname(t.getTkind().getCname());
				sub.setUserid(t.getTuserByCuserid().getCid());
				sub.setUsername(t.getTuserByCuserid().getCname());
				rows.add(sub);
			}
		}
		return rows;
	}

	private String addWhere(Ycsj ycsj, String hql, Map<String, Object> params) {
		if (ycsj != null && ycsj.getCbh() != null && !ycsj.getCbh().trim().equals("")) {
			hql += " and t.cbh = :bh ";
			params.put("bh", ycsj.getCbh());
		}
		if (ycsj != null && ycsj.getDeptIds() != null && ycsj.getDeptIds().size() > 0) {
			hql += " and t.tdept.cid in(:deptids)";
			params.put("deptids", ycsj.getDeptIds());
		}
		if (ycsj != null && ycsj.getStartMonth() != null) {
			hql += " and t.csbyf >= :startmonth";
			params.put("startmonth", ycsj.getStartMonth());
		}
		if (ycsj != null && ycsj.getEndMonth() != null) {
			hql += " and t.csbyf <= :endmonth";
			params.put("endmonth", ycsj.getEndMonth());
		}
		if (ycsj != null && !StringUtils.isEmpty(ycsj.getCzh())) {
			hql += " and t.czh like :zh ";
			params.put("zh", "%%" + ycsj.getCzh() + "%%");
		}
		return hql;
	}

	/**
	 * 根据条件查询出需要导出excel的数据
	 */
	@Override
	public List<Ycsj> excelData(Ycsj ycsj) {
		String hql = "from Tycsj t where 1=1 ";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(ycsj, hql, params);
		hql += " order by t.cbh,t.csbyf";
		List<Tycsj> ycsjs = ycsjDao.find(hql, params);
		return listRows(ycsjs);
	}

}
